package com.group6.converter;

import java.util.Stack;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.text.Editable;
import android.text.TextWatcher;

public class OctalMDAS extends Activity {
	
	EditText txtOctal, txtOctal1;
    TextView txtOctal2;
	
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.octalmdas);
        txtOctal=(EditText)findViewById(R.id.txtOctal);
        txtOctal1=(EditText)findViewById(R.id.txtOctal1);
        txtOctal2=(EditText)findViewById(R.id.txtOctal2);

        Button button = (Button) findViewById(R.id.addition);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                add(v);
            }
        });
        Button button2 = (Button) findViewById(R.id.subtraction);
        button2.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
              subtract(v);
           }
          });
        Button button3 = (Button) findViewById(R.id.multiplication);
        button3.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
              multiply(v);
           }
          });
        Button button4 = (Button) findViewById(R.id.division);
        button4.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
              divide(v);
           }
          });
        Button button5 = (Button) findViewById(R.id.Button03);
        button5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                back(v);
            }
        });
    }
    	public void add(View view){
    		calculate(txtOctal, txtOctal1, txtOctal2);
    	}
    	
        public void calculate(TextView txtView, TextView txtView1, TextView txtView2)
        {
        	if(txtOctal.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	
        	if(txtOctal1.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	String a = txtOctal.getText().toString();
        	String b = txtOctal1.getText().toString();
        	int number0 = Integer.parseInt(a);
        	int number1 = Integer.parseInt(b);
        	
        	int sum = number0 + number1;
        	String s3 = Integer.toOctalString(sum);
        	txtView2.setText(s3.toString());
        }
        
        public void subtract(View view){
        	subtraction(txtOctal, txtOctal1, txtOctal2);
        }
        
        public void subtraction(TextView txtView, TextView txtView1, TextView txtView2)
        {
        	if(txtOctal.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	
        	if(txtOctal1.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	String a = txtOctal.getText().toString();
        	String b = txtOctal1.getText().toString();
        	int number0 = Integer.parseInt(a);
        	int number1 = Integer.parseInt(b);
        	
        	int sum = number0 - number1;
        	String s3 = Integer.toOctalString(sum);
        	txtView2.setText(s3.toString());
        }
        
        public void multiply(View view){
        	multiplication(txtOctal, txtOctal1, txtOctal2);
        }
        
        public void multiplication(TextView txtView, TextView txtView1, TextView txtView2)
        {
        	if(txtOctal.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	
        	if(txtOctal1.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	String a = txtOctal.getText().toString();
        	String b = txtOctal1.getText().toString();
        	int number0 = Integer.parseInt(a);
        	int number1 = Integer.parseInt(b);
        	
        	int sum = number0 * number1;
        	String s3 = Integer.toOctalString(sum);
        	txtView2.setText(s3.toString());
        }
        
        public void divide(View view){
        	division(txtOctal, txtOctal1, txtOctal2);
        }
        
        public void division(TextView txtView, TextView txtView1, TextView txtView2)
        {
        	if(txtOctal.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	
        	if(txtOctal1.getText().toString().trim().length()==0)
            {
                txtView.setText("");
                return;
            }
        	String a = txtOctal.getText().toString();
        	String b = txtOctal1.getText().toString();
        	int number0 = Integer.parseInt(a);
        	int number1 = Integer.parseInt(b);
        	
        	int sum = number0 / number1;
        	String s3 = Integer.toOctalString(sum);
        	txtView2.setText(s3.toString());
        }
        
        public void back(View view){
        	Intent i = new Intent(OctalMDAS.this, Converter.class);
            startActivity(i);
        }
}